/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Logik.Kunde;
import Logik.Kundenquelle;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author Neikon
 */
public class Kundenverwaltung extends javax.swing.JFrame {

    /**
     * Creates new form Kundenverwaltung
     */
    public Kundenverwaltung() {
        initComponents();
    }
    Interfaces.Kundenverwaltung kv = new Kundenquelle();
    List<Kunde> kml = kv.anzeigen();

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 32767));
        lbl_Kundenver_KundenNr = new javax.swing.JLabel();
        lbl_Kundenver_Name = new javax.swing.JLabel();
        txt_Kundenver_KundenNr = new javax.swing.JTextField();
        txt_Kundenver_Name = new javax.swing.JTextField();
        txt_Kundenver_Geburtsdatum = new javax.swing.JTextField();
        lbl_Kundenver_Sperrgrund = new javax.swing.JLabel();
        lbl_Kundenver_Geburtsdatum = new javax.swing.JLabel();
        lbl_Kundenver_Sperren = new javax.swing.JLabel();
        RadioButton_Sperren = new javax.swing.JRadioButton();
        txt_Kundenver_Sperrgrund = new javax.swing.JTextField();
        but_Medienver_Suchen = new javax.swing.JButton();
        but_Medienver_Anlegen = new javax.swing.JButton();
        but_Medienver_Aendern = new javax.swing.JButton();
        but_Medienver_Loeschen = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        list_Kundenver_Kundenlsite = new javax.swing.JList();
        lbl_Kundenver_Sperrdatum = new javax.swing.JLabel();
        lbl_Kundenver_DatumSperr = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Kundenverwaltung");

        lbl_Kundenver_KundenNr.setText("KundenNr:");

        lbl_Kundenver_Name.setText("Name:");

        txt_Kundenver_KundenNr.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txt_Kundenver_KundenNrActionPerformed(evt);
            }
        });

        txt_Kundenver_Name.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txt_Kundenver_NameActionPerformed(evt);
            }
        });

        lbl_Kundenver_Sperrgrund.setText("Sperrgrund");

        lbl_Kundenver_Geburtsdatum.setText("Geburtsdatum:");

        lbl_Kundenver_Sperren.setText("Sperren:");

        RadioButton_Sperren.setText("Gesperrt");
        RadioButton_Sperren.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RadioButton_SperrenActionPerformed(evt);
            }
        });

        but_Medienver_Suchen.setText("Suchen");
        but_Medienver_Suchen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                but_Medienver_SuchenActionPerformed(evt);
            }
        });

        but_Medienver_Anlegen.setText("Anlegen");
        but_Medienver_Anlegen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                but_Medienver_AnlegenActionPerformed(evt);
            }
        });

        but_Medienver_Aendern.setText("Ändern");
        but_Medienver_Aendern.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                but_Medienver_AendernActionPerformed(evt);
            }
        });

        but_Medienver_Loeschen.setText("Löschen");
        but_Medienver_Loeschen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                but_Medienver_LoeschenActionPerformed(evt);
            }
        });

        list_Kundenver_Kundenlsite.setModel(new KundeListModel(kml));
        jScrollPane1.setViewportView(list_Kundenver_Kundenlsite);

        lbl_Kundenver_Sperrdatum.setText("Sperrdatum:");

        lbl_Kundenver_DatumSperr.setText(" ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lbl_Kundenver_KundenNr)
                                    .addComponent(lbl_Kundenver_Name))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(33, 33, 33)
                                        .addComponent(txt_Kundenver_Name, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addGap(32, 32, 32)
                                        .addComponent(txt_Kundenver_KundenNr, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addComponent(txt_Kundenver_Geburtsdatum, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(37, 37, 37)
                        .addComponent(but_Medienver_Suchen))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(but_Medienver_Anlegen)
                            .addComponent(lbl_Kundenver_Geburtsdatum))
                        .addGap(18, 18, 18)
                        .addComponent(but_Medienver_Aendern)
                        .addGap(18, 18, 18)
                        .addComponent(but_Medienver_Loeschen)))
                .addGap(60, 60, 60)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lbl_Kundenver_Sperren)
                            .addComponent(lbl_Kundenver_Sperrdatum))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(20, 20, 20)
                                .addComponent(RadioButton_Sperren))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(lbl_Kundenver_DatumSperr, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(109, 109, 109))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lbl_Kundenver_Sperrgrund)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(txt_Kundenver_Sperrgrund, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lbl_Kundenver_KundenNr)
                        .addGap(18, 18, 18)
                        .addComponent(lbl_Kundenver_Name))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txt_Kundenver_KundenNr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(but_Medienver_Suchen)
                            .addComponent(lbl_Kundenver_Sperren)
                            .addComponent(RadioButton_Sperren))
                        .addGap(14, 14, 14)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txt_Kundenver_Name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lbl_Kundenver_Sperrdatum)
                            .addComponent(lbl_Kundenver_DatumSperr))))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txt_Kundenver_Geburtsdatum)
                        .addComponent(lbl_Kundenver_Sperrgrund)
                        .addComponent(txt_Kundenver_Sperrgrund, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(lbl_Kundenver_Geburtsdatum, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(but_Medienver_Anlegen)
                            .addComponent(but_Medienver_Aendern)
                            .addComponent(but_Medienver_Loeschen)))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(54, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void but_Medienver_AnlegenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_but_Medienver_AnlegenActionPerformed
        int kundenNummer = 0;
        try {
            kundenNummer = Integer.parseInt(txt_Kundenver_KundenNr.getText());
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "Ungültige Kundennummer");
            return;
        }

        String name = txt_Kundenver_Name.getText();

        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
        Date date = null;
        try {
            date = sdf.parse(txt_Kundenver_Geburtsdatum.getText());
        } catch (ParseException e) {
            JOptionPane.showMessageDialog(this, "Ungültiges Datumsformat");
            return;
        }
        Kunde k = new Kunde(kundenNummer, name, date);
        kv.anlegen(k);
        List<Kunde> alleKunden = kv.anzeigen();
        list_Kundenver_Kundenlsite.setModel(new KundeListModel(alleKunden));



    }//GEN-LAST:event_but_Medienver_AnlegenActionPerformed

    private void txt_Kundenver_NameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txt_Kundenver_NameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txt_Kundenver_NameActionPerformed

    private void RadioButton_SperrenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RadioButton_SperrenActionPerformed
        int kundenNummer = 0;
        try {
            kundenNummer = Integer.parseInt(txt_Kundenver_KundenNr.getText());
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "Ungültige Kundennummer");
            return;
        }

        String name = txt_Kundenver_Name.getText();

        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
        Date date = null;
        try {
            date = sdf.parse(txt_Kundenver_Geburtsdatum.getText());
        } catch (ParseException e) {
            JOptionPane.showMessageDialog(this, "Ungültiges Datumsformat");
            return;
        }
        Kunde k = new Kunde(kundenNummer, name, date);

        if (RadioButton_Sperren.isSelected()) {
            String sperrgrund = txt_Kundenver_Sperrgrund.getText();
            k.sperreKunde(new Date(), sperrgrund);
        } else {
            k.entsperrenKunde();
        }
        kv.aendern(k);
        kv.speichern();


    }//GEN-LAST:event_RadioButton_SperrenActionPerformed

    private void but_Medienver_AendernActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_but_Medienver_AendernActionPerformed
        int kundenNummer = 0;
        try {
            kundenNummer = Integer.parseInt(txt_Kundenver_KundenNr.getText());
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "Ungültige Kundennummer");
            return;
        }

        String name = txt_Kundenver_Name.getText();

        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
        Date date = null;
        try {
            date = sdf.parse(txt_Kundenver_Geburtsdatum.getText());
        } catch (ParseException e) {
            JOptionPane.showMessageDialog(this, "Ungültiges Datumsformat");
            return;
        }
        Kunde k = new Kunde(kundenNummer, name, date);
        kv.loeschen(k.getkundennr());
        kv.anlegen(k);
        List<Kunde> alleKunden = kv.anzeigen();
        list_Kundenver_Kundenlsite.setModel(new KundeListModel(alleKunden));
    }//GEN-LAST:event_but_Medienver_AendernActionPerformed

    private void but_Medienver_LoeschenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_but_Medienver_LoeschenActionPerformed
        int kundenNummer = Integer.parseInt(txt_Kundenver_KundenNr.getText());
        kv.loeschen(kundenNummer);
        List<Kunde> alleKunden = kv.anzeigen();
        list_Kundenver_Kundenlsite.setModel(new KundeListModel(alleKunden));

    }//GEN-LAST:event_but_Medienver_LoeschenActionPerformed

    private void but_Medienver_SuchenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_but_Medienver_SuchenActionPerformed
        int kundenNummer = 0;
        try {
            kundenNummer = Integer.parseInt(txt_Kundenver_KundenNr.getText());
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "Ungültige Kundennummer");
            return;
        }

        List<Kunde> kunden = kv.anzeigen();
        Kunde temp = null;
        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
        for (Kunde k : kunden) {
            if (kundenNummer == k.getkundennr()) {
                temp = k;
            }
        }
        if (temp == null) {
            return;
        }
        txt_Kundenver_KundenNr.setText(String.valueOf(temp.getkundennr()));
        txt_Kundenver_Name.setText(temp.getName());
        txt_Kundenver_Geburtsdatum.setText(sdf.format(temp.getGeburtsdatum()));
        txt_Kundenver_Sperrgrund.setText(temp.getgrund());
        if (temp.getsperrStatus() == true) {
            RadioButton_Sperren.setSelected(true);
        } else {
            RadioButton_Sperren.setSelected(false);
        }
        if (temp.getsperrStatus() == true) {
            lbl_Kundenver_DatumSperr.setText(temp.getsperrDatum().toString());
        }
        List<Kunde> alleKunden = kv.anzeigen();
        list_Kundenver_Kundenlsite.setModel(new KundeListModel(alleKunden));
    }//GEN-LAST:event_but_Medienver_SuchenActionPerformed

    private void txt_Kundenver_KundenNrActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txt_Kundenver_KundenNrActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txt_Kundenver_KundenNrActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Kundenverwaltung.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Kundenverwaltung.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Kundenverwaltung.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Kundenverwaltung.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Kundenverwaltung().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton RadioButton_Sperren;
    private javax.swing.JButton but_Medienver_Aendern;
    private javax.swing.JButton but_Medienver_Anlegen;
    private javax.swing.JButton but_Medienver_Loeschen;
    private javax.swing.JButton but_Medienver_Suchen;
    private javax.swing.Box.Filler filler1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbl_Kundenver_DatumSperr;
    private javax.swing.JLabel lbl_Kundenver_Geburtsdatum;
    private javax.swing.JLabel lbl_Kundenver_KundenNr;
    private javax.swing.JLabel lbl_Kundenver_Name;
    private javax.swing.JLabel lbl_Kundenver_Sperrdatum;
    private javax.swing.JLabel lbl_Kundenver_Sperren;
    private javax.swing.JLabel lbl_Kundenver_Sperrgrund;
    private javax.swing.JList list_Kundenver_Kundenlsite;
    private javax.swing.JTextField txt_Kundenver_Geburtsdatum;
    private javax.swing.JTextField txt_Kundenver_KundenNr;
    private javax.swing.JTextField txt_Kundenver_Name;
    private javax.swing.JTextField txt_Kundenver_Sperrgrund;
    // End of variables declaration//GEN-END:variables
}
